package com.philip.journal.home.bean;

import com.philip.journal.core.bean.AbstractAuditableBean;
import com.philip.util.EqualsUtil;
import com.philip.util.HashCodeUtil;

/**
 * Represents a folder Node of a tree in the Journal Application.
 */
public class Branch extends AbstractAuditableBean {

    /** Primary key field for Branch entity. */
    private long branchId;

    /** Display name of this branch. */
    private String name;

    /** Parent branch of this Branch. */
    private Branch parent;

    /** Helper field for the given attribute. */
    public static final String ATTR_NAME = "name";
    /** Helper field for the given attribute. */
    public static final String ATTR_BRANCH_ID = "branchId";
    /** Helper field for the given attribute. */
    public static final String ATTR_PARENT = "parent";

    /** Default constructor. */
    public Branch() {
        super();
    }

    /**
     * @param pName initialize branch with name.
     * @param pNarent initialize branch with parent. Parameterized constructor.
     */
    public Branch(final String pName, final Branch pNarent) {
        this();
        this.name = pName;
        this.parent = pNarent;
    }

    /**
     * @return {@link #branchId}
     */
    public long getBranchId() {
        return this.branchId;
    }

    /**
     * @param pBranchId sets the new {@link #branchId}.
     */
    public void setBranchId(final long pBranchId) {
        this.branchId = pBranchId;
    }
    /**
     * @return {@link #name}
     */
    public String getName() {
        return this.name;
    }

    /**
     * @param pName sets the new {@link #name}.
     */
    public void setName(final String pName) {
        this.name = pName;
    }

    /**
     * @return {@link #parent}
     */
    public Branch getParent() {
        return this.parent;
    }

    /**
     * @param pParent sets the new {@link #parent}.
     */
    public void setParent(final Branch pParent) {
        this.parent = pParent;
    }

    @Override
    public String getPrimaryKeyField() {
        return "branchId";
    }

    @Override
    public boolean equals(final Object aThat) {
        boolean retval = false;
        if (this == aThat) {
            retval = true;
        } else if (aThat instanceof Branch) { // (See Effective Java by Joshua Bloch.)
            //cast to native object is now safe
            final Branch that = (Branch) aThat;
            retval = EqualsUtil.areEqual(this.branchId, that.branchId) && EqualsUtil.areEqual(this.name, that.name);
        } else {
            retval = false;
        }
        return retval;

    }

    @Override
    public int hashCode() {
        int result = HashCodeUtil.SEED;
        result = HashCodeUtil.hash(result, name);
        result = HashCodeUtil.hash(result, branchId);

        if (parent != null) {
            result = HashCodeUtil.hash(result, parent.getBranchId());
        }
        return result;
    }

}
